using System.Collections.Generic;
using System.Linq;
using SiemensAmd.Mmt.Amd.McisDbModel;
using SiemensAmd.Mmt.McisDbModel;
using SqlSugar;
using SqlSugarEx.Utilities;

namespace SiemensAmd.Mmt.McisDbDal
{
	public class TdiToolDataDal
	{
		private readonly SqlSugarClient _sqlSugar;

		private readonly SqlSugarClient _sqlSugarToolUnion;

		public TdiToolDataDal(string server, string db, string user, string pwd)
		{
			this._sqlSugar = new SqlSugarClient(new ConnectionConfig
			{
				DbType = DbType.SqlServer,
				ConnectionString = DbUtility.SQLDbFormat(server, db, user, pwd),
				IsAutoCloseConnection = false
			});
			this._sqlSugarToolUnion = new SqlSugarClient(new ConnectionConfig
			{
				DbType = DbType.SqlServer,
				ConnectionString = DbUtility.SQLDbFormat(server, db, user, pwd),
				IsAutoCloseConnection = false
			});
		}

		public TDI_TOOLDATAEntity QueryTdiToolData(string exterId)
		{
			return (from x in this._sqlSugar.Queryable<TDI_TOOLDATAEntity>()
				where x.ToolExternId == exterId
				select x).ToList().FirstOrDefault();
		}

		public List<ToolDataUnionEntity> QueryToolDataUnion(long[] pkBuff = null)
		{
			ISugarQueryable<ToolPlaceEntity, ToolDataDetailsEntity, ToolDataEntity> sugarQueryable = this._sqlSugarToolUnion.Queryable((ToolPlaceEntity a, ToolDataDetailsEntity b, ToolDataEntity c) => a.DbIdTDIToolData == b.DbIdTDIToolData && a.DbIdTDIToolData == c.DbIdTDIToolData);
			ISugarQueryable<ToolPlaceEntity, ToolDataDetailsEntity, ToolDataEntity> sugarQueryable2 = this._sqlSugarToolUnion.Queryable((ToolPlaceEntity a, ToolDataDetailsEntity b, ToolDataEntity c) => new JoinQueryInfos(JoinType.Left, a.DbIdTDIToolData == b.DbIdTDIToolData, JoinType.Left, a.DbIdTDIToolData == c.DbIdTDIToolData));
			return sugarQueryable2.Select<ToolDataUnionEntity>().MergeTable().WhereIF(pkBuff != null, (ToolDataUnionEntity x) => SqlFunc.ContainsArray(pkBuff, x.DbIdTDIToolDataDetails))
				.ToList();
		}
	}
}
